#!/usr/bin/python

import json
import socket

TCP_IP = input('IP: ')
TCP_IP = '192.168.188.65'
TCP_PORT = 8477
BUFFER_SIZE = 1024

# Connect
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))

# HELO
heloJson = '{"username":"eckhart"}'
helo = b'\x01' + bytes(heloJson, 'utf-8')
s.send(helo)
data = s.recv(BUFFER_SIZE)
print(data.decode('utf-8'))
data = data[1:]
locations = json.loads(data.decode('utf-8'))['locations']
sessions = json.loads(data.decode('utf-8'))['sessions']
print(type(locations))
print(sessions)

createO = {"location":locations[0],
          "datetime":"2017-01-01T12:23:43+0100",
          "resources": {"tobacco":False,
                        "papes":False,
                        "filters": False,
                        "lighter": False}
           }
createJson = json.dumps(createO)
create = b'\x03'+bytes(createJson, "utf-8")
s.send(create)
data = s.recv(BUFFER_SIZE)
print (data.decode('utf-8'))
data = data[1:]

try:
    while True:
        data = s.recv(BUFFER_SIZE)
        print(data.decode('utf-8'))
except KeyboardInterrupt:
    print('Closing socket')
    s.close()
